<template>
  <view>
    <!-- 搜索栏子组件 -->
    <view class="search-box">
      <my-search-bar />
    </view>
    <!-- 轮播图 -->
    <swiper indicator-dots autoplay circular :interval="3000">
      <swiper-item v-for="item in swipers" :key="item.goods_id">
        <image :src="item.image_src" />
      </swiper-item>
    </swiper>
    <!-- 菜单列表 -->
    <view class="nav-list">
      <image
        v-for="item in catitems"
        :key="item.name"
        @click="switchTab(item)"
        :src="item.image_src"
        class="nav-img"
        mode="scaleToFill"
      />
    </view>
    <!-- 楼层 -->
    <view>
      <view v-for="item in floorList" :key="item.floor_title.image_src">
        <!-- 标题区域 -->
        <image
          class="floor-title"
          :src="item.floor_title.image_src"
          mode="widthFix"
        />
        <!-- 楼层图片区域 -->
        <view class="floor-img-box">
          <!-- 左边图片 -->
          <navigator
            :url="item.product_list[0].navigator_url"
            open-type="navigate"
            hover-class="navigator-hover"
          >
            <!-- 左侧大图片的盒子 -->
            <image
              :style="{
                width: item.product_list[0].image_width + 'rpx'
              }"
              mode="widthFix"
              :src="item.product_list[0].image_src"
            />
          </navigator>
          <!-- 右边图片 -->
          <view class="right-img-box">
            <!-- 右侧 4 个小图片的盒子 -->
            <navigator
              :url="subitem.navigator_url"
              open-type="navigate"
              hover-class="navigator-hover"
              v-for="(subitem, index) in item.product_list"
              v-show="index > 0"
              :key="subitem.name"
            >
              <image
                :src="subitem.image_src"
                mode="widthFix"
                :style="{ width: subitem.image_width + 'rpx' }"
              />
            </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import request from '@/utils/resquest'
export default {
  data () {
    return {
      swipers: [], //
      catitems: [],//
      floorList: [] //  楼层的数据列表
    }
  },
  onLoad (options) {
    this.getSwieprsData()
    this.getCatitemsData()
    this.getFloorList()
  },
  methods: {
    async getSwieprsData () {
      const res = await request({
        url: 'home/swiperdata'
      })
      if (res.meta.status === 200) {
        this.swipers = res.message
      }
      // console.log(res)
    },
    async getCatitemsData () {
      const res = await request({
        url: 'home/catitems'
      })
      if (res.meta.status === 200) {
        this.catitems = res.message
      }
      // console.log(res)
    },
     async getFloorList () {
      const res = await request({
        url: 'home/floordata'
      })

      if (res.meta.status === 200) {
        res.message.forEach(item => {
          item.product_list.forEach(subitem => {
            subitem.navigator_url =
              '/subpkg/goods-list/index?' + subitem.navigator_url.split('?')[1]
          })
        })
        this.floorList = res.message
      }
    },
    switchTab ({ name }) {
      if (name !== '分类') return
      uni.switchTab({
        url: '/pages/category/index'
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.search-box {
  // 设置定位效果为“吸顶”
  position: sticky;
  // 吸顶的“位置”
  top: 0;
  // 提高层级，防止被轮播图覆盖
  z-index: 999;
}

swiper {
  height: 400rpx;

  .swiper-item,
  image {
    width: 100%;
    height: 100%;
  }
}

.nav-list {
  display: flex;
  justify-content: space-around;
  margin: 15px 0;

  .nav-img {
    width: 128rpx;
    height: 140rpx;
  }
}

.floor-title {
  height: 60rpx;
  width: 100%;
  display: flex;
}

.right-img-box {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.floor-img-box {
  display: flex;
  padding-left: 10rpx;
}
</style>
